package com.canadroid;

import java.util.ArrayList;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.Scene.ITouchArea;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

/**
 * @author Andres Quattrini
 * @since 11:54:51 - 03.04.2010
 */
public class Canadroid extends BaseGameActivity implements IOnSceneTouchListener, IOnAreaTouchListener {

        private static final int CAMERA_WIDTH = 720;
        private static final int CAMERA_HEIGHT = 480;


        private Camera mCamera;
        private Texture mTexture;
        private TextureRegion mFaceTextureRegion;
        private TextureRegion buildTexRegion;

        Guy guy;
        Building building;

        @Override
        public Engine onLoadEngine() {
                this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
                return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
        }

        @Override
        public void onLoadResources() {
                this.mTexture = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
                this.mFaceTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/boxface.png", 0, 0);
                buildTexRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/AmazingBuilding.png", 17, 0);
                this.mEngine.getTextureManager().loadTexture(this.mTexture);
        }

        @Override
        public Scene onLoadScene() {
                this.mEngine.registerUpdateHandler(new FPSLogger());

                final Scene scene = new Scene(1);
                scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

                /* Calculate the coordinates for the face, so its centered on the camera. */
                final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
                final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;

                /* Create the face and add it to the scene. */
                guy = new Guy(CAMERA_WIDTH - 100, centerY - 50, this.mFaceTextureRegion);
                building = new Building(centerX, centerY, 128, 256, this.buildTexRegion);
                //final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion);
                scene.getLastChild().attachChild(building);
                scene.getLastChild().attachChild(guy);
                
                scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
                    @Override
                    public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
                            switch(pSceneTouchEvent.getAction()) {
                                    case TouchEvent.ACTION_DOWN:
                                    		guy.Jump(100.0f);
                                            break;
                                    case TouchEvent.ACTION_UP:
                                            break;
                            }
                            return true;
                    }
            });
                
        		scene.registerUpdateHandler(new IUpdateHandler() {
        			@Override
        			public void onUpdate(float pSecondsElapsed) 
        			{
        				int i=0;
        				i++;
        			}

					@Override
					public void reset() {
						// TODO Auto-generated method stub
						
					}});
                
            return scene;
        }

        @Override
        public void onLoadComplete() {

        }
        
        @Override
        public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) 
        {
                return true;
        }

		@Override
		public boolean onAreaTouched(TouchEvent sceneTouchEvent,
				ITouchArea touchArea, float touchAreaLocalX,
				float touchAreaLocalY) {
			// TODO Auto-generated method stub
			return false;
		}
		

        


        // ===========================================================
        // Methods
        // ===========================================================

        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================
}